package xmlexample;

/***************************************************************************
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 *
 ***************************************************************************/

import java.io.Serializable;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.URL;

public class Gui extends JFrame implements ActionListener, ItemListener, Serializable {

  // Component references
  private  JButton exitButton;
  private  JTextField text;
  private  JPanel centralPanel;
  private  JPanel southPanel;
  private  JTextArea ta;
  private JComboBox comboLayout;

  private static final StringBuffer sb= new StringBuffer();

  private static UIManager.LookAndFeelInfo[] lookInfo;
  private final static String COPYRIGHT="GUI by Ermanno Franco";

  /**
   * Create the Gui JFrame
   */
  public Gui() {
    this ("",null);
  }

  public Gui(String title) {
    this (title,null);
  }

  public Gui(String title,URL iconURL) {
    String titolo=(title!=null&&title.length()>0)?title+" - "+COPYRIGHT:COPYRIGHT;
    setTitle(titolo);
    if (iconURL!=null) setIconImage(new ImageIcon(iconURL).getImage());

    buildGui();
    setListeners();

    Dimension screensize=Toolkit.getDefaultToolkit().getScreenSize();
    setSize(screensize.width,screensize.height*5/6);
    Utilita.center(this,screensize);
    show();

  }

  public Gui view(String text) {
    ta.setText(text);
    return this;
  }

  public Gui append(String text) {
    ta.setText(ta.getText()+"\r\n"+text);
    return this;
  }
  public Gui view(Object obj) {
    ta.setText(obj.toString());
    return this;
  }

  public Gui append(Object obj) {
    ta.setText(ta.getText()+"\r\n"+obj.toString());
    return this;
  }


  public void actionPerformed(ActionEvent e) {
    if (e.getSource()==exitButton) {
      System.exit(0);
      return;//che serve?
    }
  }

  public void itemStateChanged(ItemEvent e) {
    if (e.getItemSelectable()==comboLayout)  {
      changeTheLookAndFeel(comboLayout.getSelectedIndex());
    }
  }

  private void enableUI(boolean resettaColore) {
    //dialogButton.setEnabled(true);
    exitButton.setEnabled(true);
    if (resettaColore) text.setForeground(Color.BLACK);
    setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    text.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    ta.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));

  }

  private void resetGui() {
    sb.delete(0,sb.length());
    text.setText("");
    ta.setText("");

  }

  private void disableGui() {
    setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    text.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    ta.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

    exitButton.setEnabled(false);

  }

  private void buildGui() {

    buildPLAF();

    getContentPane().setLayout(new BorderLayout());
    centralPanel=new JPanel(new BorderLayout());

    // Buttons and dialog construction
    //dialogButton = new JButton("Scegli DTD...");
    //dialogButton.setToolTipText("Apre una finestra per il file dtd");
    exitButton = new JButton("Esci");
    exitButton.setToolTipText("Esce dal programma");

    //JText
    text=new JTextField();
    text.setFont(new Font(null,Font.BOLD,12));
    text.setEditable(false);

    //JTextArea
    ta=new JTextArea();
    ta.setEditable(false);

    // Add component to JPanel
    centralPanel.add(new JScrollPane(ta),BorderLayout.CENTER);

    //fine pannello grande

    //pannello in basso
    southPanel = new JPanel(new BorderLayout());
    southPanel.add(comboLayout, BorderLayout.WEST);
    southPanel.add(text, BorderLayout.CENTER);
    southPanel.add(exitButton, BorderLayout.EAST);
    //fine pannello in basso

    getContentPane().add(centralPanel, BorderLayout.CENTER);
    getContentPane().add(southPanel, BorderLayout.SOUTH);

  }


  private void setListeners()  {
    // Listeners
    exitButton.addActionListener(this);
    comboLayout.addItemListener(this);
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
  }

  private void changeTheLookAndFeel(int index) {

    try {
      UIManager.setLookAndFeel(lookInfo[index].getClassName());
      SwingUtilities.updateComponentTreeUI(this);
    }
    catch ( Exception e ) {
      e.printStackTrace();
    }
  }

  private void buildPLAF() {
    int index=-1;
    lookInfo=UIManager.getInstalledLookAndFeels();
    String[] lookInfoNames=new String[lookInfo.length];
    for (int i=0;i<lookInfoNames.length;i++)  {
      lookInfoNames[i]=lookInfo[i].getName();
      if (lookInfo[i].getClassName().equals(UIManager.getSystemLookAndFeelClassName()))
        index=i;
    }
    comboLayout=new JComboBox(lookInfoNames);
    if (index!=-1)comboLayout.setSelectedIndex(index);

    try {
      UIManager.setLookAndFeel(lookInfo[comboLayout.getSelectedIndex()].getClassName());
    } catch(Exception e)
    {System.out.println(e+": "+e.getMessage());
    }


  }

}
